package mysql;

import javafx.collections.ObservableList;
import utils.MyList;
import mysql.types.Type;

import java.util.LinkedHashMap;
import java.util.Map;

import static utils.titles.Title.fromSnakeCase;

public class Table extends MySQL.Object {
    private final MyList<Column> columns = new MyList<>();
    private final Map<String, Table> dependencies = new LinkedHashMap<>();

    private Schema schema;

    public Table(Schema schema, String name) {
        super(fromSnakeCase(name));
        this.schema = schema;
        schema.getTables().add(this);
    }

    public Column createColumn(String field, Type type, boolean isNull, String key, String defaultValue, String extra) {
        Column column = new Column(this, field, type, isNull, key, defaultValue, extra);

        columns.add(column);

        return column;
    }

    public ObservableList<Column> getColumnList() {
        return columns.getList();
    }

    public Column getColumn(String name) {
        return columns.get(name);
    }

    public Table[] getDependencyTables() {
        return dependencies.values().toArray(new Table[dependencies.size()]);
    }

    public void addDependency(String name, Table table) {
        dependencies.put(name, table);
    }

    public Schema getSchema() {
        return schema;
    }
}
